#!/bin/bash
# Advanced功能演示Script

echo "=========================================="
echo "  STM32 Firmware Side-Channel Attack - Advanced功能演示"
echo "=========================================="
echo ""

# CheckYesNoHas预ProcessTrace
TRACE_FILE="stm32_aes/traces_preprocessed.npz"
PLAINTEXT_FILE="stm32_aes/bin/aes_test_data.h"

if [ ! -f "$TRACE_FILE" ]; then
    echo "Error: 未找ToTraceFile $TRACE_FILE"
    echo "请先Run: cd stm32_aes && python3 collect_traces.py --traces 100"
    exit 1
fi

echo "UseTraceFile: $TRACE_FILE"
echo ""

# Example1: 快速Verify（单字节Attack）
echo "=========================================="
echo "Example1: 快速Verify - 单字节CPAAttack"
echo "=========================================="
echo "Command: python3 analyze_firmware_traces.py $TRACE_FILE --byte-range 0,1 --attacks cpa"
echo ""
read -p "Execute? (y/n): " answer
if [ "$answer" = "y" ]; then
    python3 analyze_firmware_traces.py "$TRACE_FILE" \
        --byte-range 0,1 \
        --attacks cpa
    echo ""
fi

# Example2: UseTrue实Plaintext（If存In）
if [ -f "$PLAINTEXT_FILE" ]; then
    echo "=========================================="
    echo "Example2: UseTrue实Plaintext - 前4字节Attack"
    echo "=========================================="
    echo "Command: python3 analyze_firmware_traces.py $TRACE_FILE"
    echo "      --plaintexts $PLAINTEXT_FILE"
    echo "      --byte-range 0,4 --attacks cpa --verbose"
    echo ""
    read -p "Execute? (y/n): " answer
    if [ "$answer" = "y" ]; then
        python3 analyze_firmware_traces.py "$TRACE_FILE" \
            --plaintexts "$PLAINTEXT_FILE" \
            --byte-range 0,4 \
            --attacks cpa \
            --verbose
        echo ""
    fi
else
    echo "Hint: 未找ToPlaintextFile $PLAINTEXT_FILE"
    echo "      Require先GenerateTestData并编译固件"
    echo ""
fi

# Example3: Not同Leakage model对比
echo "=========================================="
echo "Example3: Leakage model对比 (单字节快速Test)"
echo "=========================================="
echo "Test hamming_weight And hamming_distance 模型"
echo ""
read -p "Execute? (y/n): " answer
if [ "$answer" = "y" ]; then
    echo ""
    echo "--- 汉明权重模型 ---"
    python3 analyze_firmware_traces.py "$TRACE_FILE" \
        --leakage-model hamming_weight \
        --byte-range 0 1 \
        --attacks cpa
    
    echo ""
    echo "--- Hamming distance模型 ---"
    python3 analyze_firmware_traces.py "$TRACE_FILE" \
        --leakage-model hamming_distance \
        --byte-range 0 1 \
        --attacks cpa
    echo ""
fi

# Example4: 多种AttackMethod对比
echo "=========================================="
echo "Example4: AttackMethod对比 (CPA vs DPA)"
echo "=========================================="
echo "Command: python3 analyze_firmware_traces.py $TRACE_FILE"
echo "      --byte-range 0,4 --attacks cpa dpa"
echo ""
read -p "Execute? (y/n): " answer
if [ "$answer" = "y" ]; then
    python3 analyze_firmware_traces.py "$TRACE_FILE" \
        --byte-range 0,4 \
        --attacks cpa dpa
    echo ""
fi

# Example5: SaveResult
echo "=========================================="
echo "Example5: SaveAttackResult"
echo "=========================================="
echo "Command: python3 analyze_firmware_traces.py $TRACE_FILE"
echo "      --byte-range 0,4 --attacks cpa"
echo "      --save-result demo_results.npz"
echo ""
read -p "Execute? (y/n): " answer
if [ "$answer" = "y" ]; then
    python3 analyze_firmware_traces.py "$TRACE_FILE" \
        --byte-range 0,4 \
        --attacks cpa \
        --save-result demo_results.npz
    
    if [ -f "demo_results.npz" ]; then
        echo ""
        echo "Result已SaveTo demo_results.npz"
        echo "查看内容:"
        python3 -c "import numpy as np; d=np.load('demo_results.npz'); print('\\n'.join(f'{k}: {v}' for k,v in d.items()))"
    fi
    echo ""
fi

echo "=========================================="
echo "  演示Completed"
echo "=========================================="
echo ""
echo "Hint:"
echo "- Use --verbose 查看DetailedOutput"
echo "- Use --plaintexts LoadTrue实Plaintext以提高Success率"
echo "- Use --byte-range 0,1 进行快速Test（单字节）"
echo "- Use --byte-range 0,4 Test前4个字节"
echo "- 查看 ADVANCED_USAGE_GUIDE.md 了解更多Options"
